package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Favorite;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteMapper {

    // 插入收藏记录
    @Insert("INSERT INTO favorites (userid, target_id, target_type) " +
            "VALUES (#{userId}, #{targetId}, #{targetType})")
    void insertFavorite(Favorite favorite);

    // 查询用户的收藏记录
    @Select("SELECT * FROM favorites WHERE userid = #{userId}")
    List<Favorite> findFavoritesByUserId(Integer userId);

    // 删除收藏记录
    @Delete("DELETE FROM favorites WHERE userid = #{favorite.userId} AND target_id = #{favorite.targetId} AND target_type = #{favorite.targetType}")
    void deleteFavorite(@Param("favorite") Favorite favorite);

    // 判断用户是否收藏某个帖子
    @Select("SELECT COUNT(*) FROM favorites WHERE userid = #{userId} AND target_id = #{targetId} AND target_type = 'POST'")
    int isPostFavorited(@Param("userId") Integer userId, @Param("targetId") Integer targetId);
}
